/*

  Rapunzel.pde
  
  Short story Rapunzel, taken from gutenberg.org
  Copyright expired at least for US and Germany.
  See www.gutenberg.org for more information.
  
  New LiquidCrystal 16x4 example

  m2tklib = Mini Interative Interface Toolkit Library
  
  Copyright (C) 2013  olikraus@gmail.com

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/*
    New LiquidCrystal 
    https://bitbucket.org/fmalpartida/new-liquidcrystal/
  
    Constructor				Include
    
    LiquidCrystal			#include <LiquidCrystal.h>
    LiquidCrystal_I2C		#include <LiquidCrystal_I2C.h>
    LiquidCrystal_SR			#include <LiquidCrystal_SR.h>
    LiquidCrystal_SR2W		#include <LiquidCrystal_SR2W.h>
    LiquidCrystal_SR3W		#include <LiquidCrystal_SR3W.h>
*/

#include <LiquidCrystal.h>
#include "M2tk.h"
#include "m2ghnlc.h"

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

uint8_t uiKeySelectPin = 10;
uint8_t uiKeyNextPin = 9;


//m2_rom_char part1[] M2_SECTION_PROGMEM = "1\n2\n3\n4\n5\n6\n" ;
//const m2_rom_char part1[] = "1\n2\n3\n4\n5\n6\n" ;
//typedef char PROGMEM pgm_char;

char part1[] M2_SECTION_PROGMEM =
"RAPUNZEL\n"
"\n"
"There were\n"
"once a man\n"
"and a woman\n"
"who had long\n"
"in vain\n"
"wished for a\n"
"child. At\n"
"length the\n"
"woman hoped\n"
"that God was\n"
"about to\n"
"grant her\n"
"desire. These\n"
"people had a\n"
"little window\n"
"at the back\n"
"of their\n"
"house from\n"
"which a\n"
"splendid\n"
"garden could\n"
"be seen,\n"
"which was\n"
"full of the\n"
"most\n"
"beautiful\n"
"flowers and\n"
"herbs. It\n"
"was, however,\n"
"surrounded by\n"
"a high wall,\n"
"and no one\n"
"dared to go\n"
"into it\n"
"because it\n"
"belonged to\n"
"an\n"
"enchantress,\n"
"who had great\n"
"power and was\n"
"dreaded by\n"
"all the\n"
"world. One\n"
"day the woman\n"
"was standing\n"
"by this\n"
"window and\n"
"looking down\n"
"into the\n"
"garden, when\n"
"she saw a bed\n"
"which was\n"
"planted with\n"
"the most\n"
"beautiful\n"
"rampion\n"
"(rapunzel),\n"
"and it looked\n"
"so fresh and\n"
"green that\n"
"she longed\n"
"for it, she\n"
"quite pined\n"
"away, and\n"
"began to look\n"
"pale and\n"
"miserable.\n"
"Then her\n"
"husband was\n"
"alarmed, and\n"
"asked: 'What\n"
"ails you,\n"
"dear wife?'\n"
"'Ah,' she\n"
"replied, 'if\n"
"I can't eat\n"
"some of the\n"
"rampion,\n"
"which is in\n"
"the garden\n"
"behind our\n"
"house, I\n"
"shall die.'\n"
"The man, who\n"
"loved her,\n"
"thought:\n"
"'Sooner than\n"
"let your wife\n"
"die, bring\n"
"her some of\n"
"the rampion\n"
"yourself, let\n"
"it cost what\n"
"it will.' At\n"
"twilight, he\n"
"clambered\n"
"down over the\n"
"wall into the\n"
"garden of the\n"
"enchantress,\n"
"hastily\n"
"clutched a\n"
"handful of\n"
"rampion, and\n"
"took it to\n"
"his wife. She\n"
"at once made\n"
"herself a\n"
"salad of it,\n"
"and ate it\n"
"greedily. It\n"
"tasted so\n"
"good to\n"
"her--so very\n"
"good, that\n"
"the next day\n"
"she longed\n"
"for it three\n"
"times as much\n"
"as before. If\n"
"he was to\n"
"have any\n"
"rest, her\n"
"husband must\n"
"once more\n"
"descend into\n"
"the garden.\n"
"In the gloom\n"
"of evening\n"
"therefore, he\n"
"let himself\n"
"down again;\n"
"but when he\n"
"had clambered\n"
"down the wall\n"
"he was\n"
"terribly\n"
"afraid, for\n"
"he saw the\n"
"enchantress\n"
"standing\n"
"before him.\n"
"'How can you\n"
"dare,' said\n"
"she with\n"
"angry look,\n"
"'descend into\n"
"my garden and\n"
"steal my\n"
"rampion like\n"
"a thief? You\n"
"shall suffer\n"
"for it!'\n"
"'Ah,'\n"
"answered he,\n"
"'let mercy\n"
"take the\n"
"place of\n"
"justice, I\n"
"only made up\n"
"my mind to do\n"
"it out of\n"
"necessity. My\n"
"wife saw your\n"
"rampion from\n"
"the window,\n"
"and felt such\n"
"a longing for\n"
"it that she\n"
"would have\n"
"died if she\n"
"had not got\n"
"some to eat.'\n"
"Then the\n"
"enchantress\n"
"allowed her\n"
"anger to be\n"
"softened, and\n"
"said to him:\n"
"'If the case\n"
"be as you\n"
"say, I will\n"
"allow you to\n"
"take away\n"
"with you as\n"
"much rampion\n"
"as you will,\n"
"only I make\n"
"one\n"
"condition,\n"
"you must give\n"
"me the child\n"
"which your\n"
"wife will\n"
"bring into\n"
"the world; it\n"
"shall be well\n"
"treated, and\n"
"I will care\n"
"for it like a\n"
"mother.' The\n"
"man in his\n"
"terror\n"
"consented to\n"
"everything,\n"
"and when the\n"
"woman was\n"
"brought to\n"
"bed, the\n"
"enchantress\n"
"appeared at\n"
"once, gave\n"
"the child the\n"
"name of\n"
"Rapunzel, and\n"
"took it away\n"
"with her.\n"
"\n"
;

char part2[] M2_SECTION_PROGMEM =
"Rapunzel grew\n"
"into the most\n"
"beautiful\n"
"child under\n"
"the sun. When\n"
"she was\n"
"twelve years\n"
"old, the\n"
"enchantress\n"
"shut her into\n"
"a tower,\n"
"which lay in\n"
"a forest, and\n"
"had neither\n"
"stairs nor\n"
"door, but\n"
"quite at the\n"
"top was a\n"
"little\n"
"window. When\n"
"the\n"
"enchantress\n"
"wanted to go\n"
"in, she\n"
"placed\n"
"herself\n"
"beneath it\n"
"and cried:\n"
"\n"
" 'Rapunzel,\n"
"  Rapunzel,\n"
"  Let down\n"
"  your hair\n"
"  to me.'\n"
"\n"
"Rapunzel had\n"
"magnificent\n"
"long hair,\n"
"fine as spun\n"
"gold, and\n"
"when she\n"
"heard the\n"
"voice of the\n"
"enchantress\n"
"she\n"
"unfastened\n"
"her braided\n"
"tresses,\n"
"wound them\n"
"round one of\n"
"the hooks of\n"
"the window\n"
"above, and\n"
"then the hair\n"
"fell twenty\n"
"ells down,\n"
"and the\n"
"enchantress\n"
"climbed up by\n"
"it.\n"
"\n"
"After a year\n"
"or two, it\n"
"came to pass\n"
"that the\n"
"king's son\n"
"rode through\n"
"the forest\n"
"and passed by\n"
"the tower.\n"
"Then he heard\n"
"a song, which\n"
"was so\n"
"charming that\n"
"he stood\n"
"still and\n"
"listened.\n"
"This was\n"
"Rapunzel, who\n"
"in her\n"
"solitude\n"
"passed her\n"
"time in\n"
"letting her\n"
"sweet voice\n"
"resound. The\n"
"king's son\n"
"wanted to\n"
"climb up to\n"
"her, and\n"
"looked for\n"
"the door of\n"
"the tower,\n"
"but none was\n"
"to be found.\n"
"He rode home,\n"
"but the\n"
"singing had\n"
"so deeply\n"
"touched his\n"
"heart, that\n"
"every day he\n"
"went out into\n"
"the forest\n"
"and listened\n"
"to it. Once\n"
"when he was\n"
"thus standing\n"
"behind a\n"
"tree, he saw\n"
"that an\n"
"enchantress\n"
"came there,\n"
"and he heard\n"
"how she\n"
"cried:\n"
"\n"
" 'Rapunzel,\n"
"  Rapunzel,\n"
"  Let down\n"
"  your hair\n"
"  to me.'\n"
"\n"
"Then Rapunzel\n"
"let down the\n"
"braids of her\n"
"hair, and the\n"
"enchantress\n"
"climbed up to\n"
"her. 'If that\n"
"is the ladder\n"
"by which one\n"
"mounts, I too\n"
"will try my\n"
"fortune,'\n"
"said he, and\n"
"the next day\n"
"when it began\n"
"to grow dark,\n"
"he went to\n"
"the tower and\n"
"cried:\n"
"\n"
" 'Rapunzel,\n"
"  Rapunzel,\n"
"  Let down\n"
"  your hair\n"
"  to me.'\n"
"\n"
"Immediately\n"
"the hair fell\n"
"down and the\n"
"king's son\n"
"climbed up.\n"
"\n"
"At first\n"
"Rapunzel was\n"
"terribly\n"
"frightened\n"
"when a man,\n"
"such as her\n"
"eyes had\n"
"never yet\n"
"beheld, came\n"
"to her; but\n"
"the king's\n"
"son began to\n"
"talk to her\n"
"quite like a\n"
"friend, and\n"
"told her that\n"
"his heart had\n"
"been so\n"
"stirred that\n"
"it had let\n"
"him have no\n"
"rest, and he\n"
"had been\n"
"forced to see\n"
"her. Then\n"
"Rapunzel lost\n"
"her fear, and\n"
"when he asked\n"
"her if she\n"
"would take\n"
"him for her\n"
"husband, and\n"
"she saw that\n"
"he was young\n"
"and handsome,\n"
"she thought:\n"
"'He will love\n"
"me more than\n"
"old Dame\n"
"Gothel does';\n"
"and she said\n"
"yes, and laid\n"
"her hand in\n"
"his. She\n"
"said: 'I will\n"
"willingly go\n"
"away with\n"
"you, but I do\n"
"not know how\n"
"to get down.\n"
"Bring with\n"
"you a skein\n"
"of silk every\n"
"time that you\n"
"come, and I\n"
"will weave a\n"
"ladder with\n"
"it, and when\n"
"that is ready\n"
"I will\n"
"descend, and\n"
"you will take\n"
"me on your\n"
"horse.' They\n"
"agreed that\n"
"until that\n"
"time he\n"
"should come\n"
"to her every\n"
"evening, for\n"
"the old woman\n"
"came by day.\n"
;

char part3[] M2_SECTION_PROGMEM =
"The\n"
"enchantress\n"
"remarked\n"
"nothing of\n"
"this, until\n"
"once Rapunzel\n"
"said to her:\n"
"'Tell me,\n"
"Dame Gothel,\n"
"how it\n"
"happens that\n"
"you are so\n"
"much heavier\n"
"for me to\n"
"draw up than\n"
"the young\n"
"king's\n"
"son--he is\n"
"with me in a\n"
"moment.' 'Ah!\n"
"you wicked\n"
"child,' cried\n"
"the\n"
"enchantress.\n"
"'What do I\n"
"hear you say!\n"
"I thought I\n"
"had separated\n"
"you from all\n"
"the world,\n"
"and yet you\n"
"have deceived\n"
"me!' In her\n"
"anger she\n"
"clutched\n"
"Rapunzel's\n"
"beautiful\n"
"tresses,\n"
"wrapped them\n"
"twice round\n"
"her left\n"
"hand, seized\n"
"a pair of\n"
"scissors with\n"
"the right,\n"
"and snip,\n"
"snap, they\n"
"were cut off,\n"
"and the\n"
"lovely braids\n"
"lay on the\n"
"ground. And\n"
"she was so\n"
"pitiless that\n"
"she took poor\n"
"Rapunzel into\n"
"a desert\n"
"where she had\n"
"to live in\n"
"great grief\n"
"and misery.\n"
"\n"
"On the same\n"
"day that she\n"
"cast out\n"
"Rapunzel,\n"
"however, the\n"
"enchantress\n"
"fastened the\n"
"braids of\n"
"hair, which\n"
"she had cut\n"
"off, to the\n"
"hook of the\n"
"window, and\n"
"when the\n"
"king's son\n"
"came and\n"
"cried:\n"
"\n"
" 'Rapunzel,\n"
"  Rapunzel,\n"
"  Let down\n"
"  your hair\n"
"  to me.'\n"
"\n"
"she let the\n"
"hair down.\n"
"The king's\n"
"son ascended,\n"
"but instead\n"
"of finding\n"
"his dearest\n"
"Rapunzel, he\n"
"found the\n"
"enchantress,\n"
"who gazed at\n"
"him with\n"
"wicked and\n"
"venomous\n"
"looks. 'Aha!'\n"
"she cried\n"
"mockingly,\n"
"'you would\n"
"fetch your\n"
"dearest, but\n"
"the beautiful\n"
"bird sits no\n"
"longer\n"
"singing in\n"
"the nest; the\n"
"cat has got\n"
"it, and will\n"
"scratch out\n"
"your eyes as\n"
"well.\n"
"Rapunzel is\n"
"lost to you;\n"
"you will\n"
"never see her\n"
"again.' The\n"
"king's son\n"
"was beside\n"
"himself with\n"
"pain, and in\n"
"his despair\n"
"he leapt down\n"
"from the\n"
"tower. He\n"
"escaped with\n"
"his life, but\n"
"the thorns\n"
"into which he\n"
"fell pierced\n"
"his eyes.\n"
"Then he\n"
"wandered\n"
"quite blind\n"
"about the\n"
"forest, ate\n"
"nothing but\n"
"roots and\n"
"berries, and\n"
"did naught\n"
"but lament\n"
"and weep over\n"
"the loss of\n"
"his dearest\n"
"wife. Thus he\n"
"roamed about\n"
"in misery for\n"
"some years,\n"
"and at length\n"
"came to the\n"
"desert where\n"
"Rapunzel,\n"
"with the\n"
"twins to\n"
"which she had\n"
"given birth,\n"
"a boy and a\n"
"girl, lived\n"
"in\n"
"wretchedness.\n"
"He heard a\n"
"voice, and it\n"
"seemed so\n"
"familiar to\n"
"him that he\n"
"went towards\n"
"it, and when\n"
"he\n"
"approached,\n"
"Rapunzel knew\n"
"him and fell\n"
"on his neck\n"
"and wept. Two\n"
"of her tears\n"
"wetted his\n"
"eyes and they\n"
"grew clear\n"
"again, and he\n"
"could see\n"
"with them as\n"
"before. He\n"
"led her to\n"
"his kingdom\n"
"where he was\n"
"joyfully\n"
"received, and\n"
"they lived\n"
"for a long\n"
"time\n"
"afterwards,\n"
"happy and\n"
"contented.\n"
"\n"
;

extern M2tk m2;
M2_EXTERN_ALIGN(el_top);

uint8_t total_lines1 = 0;
uint8_t first_visible_line1 = 0;

uint8_t total_lines2 = 0;
uint8_t first_visible_line2 = 0;

uint8_t total_lines3 = 0;
uint8_t first_visible_line3 = 0;

void goto_top_fn(m2_el_fnarg_p fnarg) {
  m2.setRoot(&el_top);
}

M2_INFOP(el_info1, "w13l4", &first_visible_line1, &total_lines1, part1, goto_top_fn);
M2_VSB(el_vsb1, "w1r1l4", &first_visible_line1, &total_lines1);
M2_LIST(el_list1) = { &el_info1, &el_vsb1};
M2_HLIST(el_part1, NULL, el_list1);

M2_INFOP(el_info2, "w13l4", &first_visible_line2, &total_lines2, part2, goto_top_fn);
M2_VSB(el_vsb2, "w1r1l4", &first_visible_line2, &total_lines2);
M2_LIST(el_list2) = { &el_info2, &el_vsb2};
M2_HLIST(el_part2, NULL, el_list2);

M2_INFOP(el_info3, "w13l4", &first_visible_line3, &total_lines3, part3, goto_top_fn);
M2_VSB(el_vsb3, "w1r1l4", &first_visible_line3, &total_lines3);
M2_LIST(el_list3) = { &el_info3, &el_vsb3};
M2_HLIST(el_part3, NULL, el_list3);

M2_LABEL(el_goto_title, NULL, "Rapunzel");
M2_ROOT(el_goto_part1, NULL, "Part 1", &el_part1);
M2_ROOT(el_goto_part2, NULL, "Part 2", &el_part2);
M2_ROOT(el_goto_part3, NULL, "Part 3", &el_part3);
M2_LIST(list_menu) = {&el_goto_title, &el_goto_part1, &el_goto_part2, &el_goto_part3};
M2_VLIST(el_menu_vlist, NULL, list_menu);
M2_ALIGN(el_top, "W64H64", &el_menu_vlist);
M2tk m2(&el_top, m2_es_arduino, m2_eh_4bs, m2_gh_nlc);

void setup() {
  m2_SetNewLiquidCrystal(&lcd, 16, 4);
  m2.setPin(M2_KEY_SELECT, uiKeySelectPin);
  m2.setPin(M2_KEY_NEXT, uiKeyNextPin);
}

void loop() {
  m2.checkKey();
  m2.checkKey();
  if ( m2.handleKey() )
    m2.draw();
  m2.checkKey();
}
